/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package siteanaliz;
import java.lang.StringBuffer;
import java.net.MalformedURLException;
import java.net.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.*;
import java.lang.Thread;
import java.util.Random;
/**
 *
 * @author Никита
 */
public class Searcher {
    private static String Google = "https://www.google.com/search?hl=en&num=100&start=0&lr=lang_en&q="; 
    private static String GoogleEN = "https://www.google.com/search?hl=en&num=100&start=0&lr=lang_en&q="; 
    private static String GoogleRU = "https://www.google.com/search?hl=ru&num=100&start=0&lr=lang_ru&q="; 
    private static String YndexAll = "http://www.yandex.ru/yandsearch?lr=10000&numdoc=50&p=0&text="; 
    private static String RamblerRU="http://nova.rambler.ru/search?dlang=1&mimex=0&st_date=&end_date=&news=0&limitcontext=0&exclude=&filter=&sort=3&pagelen=50&adult=soft&page=1&query=";
    private static String RamblerEN="http://nova.rambler.ru/search?dlang=2&mimex=0&st_date=&end_date=&news=0&limitcontext=0&exclude=&filter=&sort=3&pagelen=50&adult=soft&page=1&query=";
    private String PageLoad;
    private static boolean YActive;
    private static boolean GActive;
    private int region;
    private static Object locker=new Object();
    
    public Searcher(){};
  
    public Searcher(int S_Engine2)////void makeOptions(int S_Engine2)
    {
        if(S_Engine2==1) this.PageLoad=this.Google;
        if(S_Engine2==2) this.PageLoad=this.YndexAll;
        if(S_Engine2==3) this.PageLoad=this.RamblerRU;
    }
    public Searcher(String  S_Engine3, String LangA)////void makeOptions(int S_Engine2)
    {
        if(S_Engine3.equals("Google"))
        {
            if(LangA.equals("RU"))  this.PageLoad=this.GoogleRU;
            else this.PageLoad=this.GoogleEN;
        }
        
        if(S_Engine3.equals("Yandex")) 
        { 
            if(LangA.equals("RU")) this.PageLoad=this.YndexAll;
            else this.PageLoad=this.YndexAll;
        }
          if(S_Engine3.equals("Rambler")) 
        { 
            if(LangA.equals("RU")) this.PageLoad=this.RamblerRU;
            else this.PageLoad=this.RamblerEN;
        }
        
    }
    
    public String makeRequest(String r) throws MalformedURLException, IOException, InterruptedException 
    {
        
        synchronized (locker)
        {
            URL u =new URL(this.PageLoad+r);
            HttpURLConnection uc = (HttpURLConnection) u.openConnection();
            uc.setRequestMethod("GET");
            uc.setRequestProperty("User-Agent", "Mozilla/5.0");
           // uc.setRequestProperty("User-Agent", "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.9.168 Version/11.52");
            BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));

            StringBuffer sb = new StringBuffer();
            char[] buf = new char[1024];
            int x;
            while (true) {
            x = br.read(buf);
            if (x==-1) {
                br.close();
                break;
                }
            if (x==1024) sb.append(buf);
            if (x<1024) sb.append(buf, 0, x);
            }
            Random rand = new Random();
            Thread.currentThread().sleep(rand.nextInt(8000) + 5000);
            System.out.println(Thread.currentThread().getName()+" sleep 5-8 sec");
            return sb.toString();  
        }
         
    }
    
    // Проверить формат URL.
    public  URL URLchecker(String url)
    {
        // только адреса HTTP.
        if (!url.toLowerCase().startsWith("http://")) return null;
        // Проверить формат URL.
        URL CheckedURL = null;
        try 
        {
            CheckedURL = new URL(url);
        } 
        catch (Exception e) {
             return null;
        }
        return CheckedURL;
     }
   
    public String GetSimEnought()
    {
         return "<<#$%enought%$#>>";
    }
    
    public boolean CorretTag(String TxtArea)
    {
        if(TxtArea.trim().equals("")) return false;
        else return true;
    }
    
    public String GetStyleRequestString(String tag)
    {
         return tag.replaceAll("[\\s]{2,}", " ").replaceAll(" ","+");
        
    }
    public String GetEnginePage()
    {
        return PageLoad;
    }
    
    public void LoadingFrom(int startpose)
    {
        int page=startpose/50;
        if(PageLoad.indexOf("www.google.com")!=-1)
        {
            PageLoad=PageLoad.replaceAll("&start="+Integer.toString(startpose-100), "&start="+Integer.toString(startpose));
        }
        else
        {
            if(PageLoad.indexOf("www.yandex.ru")!=-1) 
               PageLoad=PageLoad.replaceAll("&p="+Integer.toString(page-1), "&p="+Integer.toString(page));
            else
            {
                 if(PageLoad.indexOf("nova.rambler.ru")!=-1) 
                    PageLoad=PageLoad.replaceAll("&page="+Integer.toString(page), "&page="+Integer.toString(page+1));
            }
           
        }
    }
}
